package com.min.base;

import com.min.entity.Person;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;

import java.beans.BeanInfo;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by llx on 2016/12/10.
 */
public class Introspector {

    public static void main(String[] args) throws Exception {
        Class<Person> person = (Class<Person>) Class.forName("com.min.entity.Person");

        try {
           Field filed = person.getDeclaredField("id");
           Person p = person.newInstance();
           System.out.println(filed.isAccessible());
           filed.setAccessible(true);
           filed.set(p,"123456");
           System.out.println(p.getId());
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }


        BeanInfo beanInfo= java.beans.Introspector.getBeanInfo(Class.forName("com.min.entity.Person"));

        PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
        for(int i=0;i<descriptors.length;i++){
            System.out.println(descriptors[i]);
        }
        Person person1 = new Person();
        String date = "2016-12-19";
        ConvertUtils.register(new Converter() {
            public Date convert(Class type, Object date) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    return sdf.parse((String)date);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        }, Date.class);
        BeanUtils.setProperty(person1,"id","0000");
        BeanUtils.setProperty(person1,"age","13");
        BeanUtils.setProperty(person1,"brith",date);

        System.out.println(person1);
    }

}
